//
//  NYOtherUserViewController.m
//  Nooryes
//
//  Created by baken on 2020/2/26.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYOtherUserViewController.h"
#import "NYTopicViewModel.h"
#import "NYRecommendCollectionViewCell.h"
#import "CHTCollectionViewWaterfallLayout.h"
#import "NYArticleListModel.h"
#import "NYTopicListTableViewCell.h"
#import "UIImage+Render.h"
#import "NYSettingViewController.h"
#import "NYLoginViewController.h"
#import "AccountModel.h"
#import "NYArticleDetailViewController.h"
#import "NYAlertView.h"


@interface NYOtherUserViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,CHTCollectionViewDelegateWaterfallLayout,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *avatar;

@property (weak, nonatomic) IBOutlet UIButton *articleBtn;
@property (weak, nonatomic) IBOutlet UILabel *accountDiscription;
@property (weak, nonatomic) IBOutlet UIButton *commentBtn;


@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;

@property (weak, nonatomic) IBOutlet UIView *redLine;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NYTopicViewModel *topicVM;

@property (nonatomic, strong) UIButton *selectedBtn;

@property (nonatomic, strong) UILabel *nameLabel;

@property (nonatomic, strong) UILabel *idLabel;
@end

@implementation NYOtherUserViewController

-(NYTopicViewModel *)topicVM{
    if (!_topicVM) {
        _topicVM = [NYTopicViewModel new];
    }
    return _topicVM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = (kScreenWidth-24-6)*0.33 + 35;
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
    
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[NYRecommendCollectionViewCell class] forCellWithReuseIdentifier:kNYRecommendCollectionViewCell];
    
    
    
    UIView* titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 25)];
    nameLabel.textColor = UIColorFromRGB(0x333333);
    nameLabel.font = [UIFont boldSystemFontOfSize:16];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    self.nameLabel = nameLabel;
    [titleView addSubview:nameLabel];
    UILabel *idLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 200, 15)];
    idLabel.font = [UIFont systemFontOfSize:12];
    idLabel.textColor = UIColorFromRGB(0x999999);
    idLabel.textAlignment = NSTextAlignmentCenter;
    self.idLabel = idLabel;
    [titleView addSubview:idLabel];
    self.navigationItem.titleView = titleView;
    
    self.topicVM.userid = self.userID;
    [self.topicVM getOtherUserInfoWithCompletionHandle:^(NSError *error) {
        if (!error) {
            [self.avatar sd_setImageWithURL:[NSURL URLWithString:self.topicVM.otherUser.image] placeholderImage:[UIImage imageNamed:@"sign_logo"]];
            self.nameLabel.text = self.topicVM.otherUser.nickname;
            self.idLabel.text = [NSString stringWithFormat:@"ID:%@",self.topicVM.otherUser.no];
            self.accountDiscription.text =self.topicVM.otherUser.intro;
        }
    }];
    
    self.topicVM.order = @"0";
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.topicVM refreshOtherArticlesCompletionHandle:^(NSError *error) {
            [self.collectionView reloadData];
            [self.collectionView.mj_header endRefreshing];
        }];
        
    }];
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self.topicVM getMoreOtherArticlesCompletionHandle:^(NSError *error) {
            [self.collectionView reloadData];
            [self.collectionView.mj_footer endRefreshing];
        }];
        
    }];
    
    [self.collectionView.mj_header beginRefreshing];
    self.selectedBtn = self.articleBtn;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNameWithOriginal:@"tit_more"] style:UIBarButtonItemStylePlain target:self action:@selector(onReoprt)];
}


- (IBAction)onArtical:(id)sender {
    self.selectedBtn.selected = NO;
    UIButton *btn = (UIButton *)sender;
    btn.selected = YES;
    self.selectedBtn = btn;
    self.redLine.center = CGPointMake(self.articleBtn.center.x, self.redLine.center.y);
    
    [UIView animateWithDuration:0.2 animations:^{
        [self.view layoutIfNeeded];
    }];
    self.collectionView.hidden = NO;
    self.tableView.hidden = YES;
    
}
-(void)onReoprt{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"用户违规" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"举报用户冒充他人" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NYAlertView *alert = [NYAlertView sharedAlertView];
        alert.title = @"收到请求，处理中!";
        [alert showAlert:^{}];
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"举报用户垃圾广告账号" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NYAlertView *alert = [NYAlertView sharedAlertView];
        alert.title = @"收到请求，处理中!";
        [alert showAlert:^{}];
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"举报用户个人信息不规范" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NYAlertView *alert = [NYAlertView sharedAlertView];
        alert.title = @"收到请求，处理中!";
        [alert showAlert:^{}];
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"举报用户辱骂他人" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NYAlertView *alert = [NYAlertView sharedAlertView];
        alert.title = @"收到请求，处理中!";
        [alert showAlert:^{}];
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"加入黑名单" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSMutableDictionary *dict = [NSMutableDictionary new];
        [dict setValue:self.topicVM.otherUser.nickname forKey:@"name"];
        [dict setValue:self.topicVM.otherUser.image forKey:@"avatar"];
        [dict setValue:self.topicVM.otherUser.no forKey:@"no"];
        NSMutableArray *array = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"blacklist"];
        if (!array) {
            array = [NSMutableArray array];
        }
        [array addObject:dict];
        NYAlertView *alert = [NYAlertView sharedAlertView];
        alert.title = @"已加入黑名单!";
        [alert showAlert:^{}];
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (IBAction)onComment:(id)sender {
    self.selectedBtn.selected = NO;
    UIButton *btn = (UIButton *)sender;
    btn.selected = YES;
    self.selectedBtn = btn;
    self.redLine.center = CGPointMake(self.commentBtn.center.x, self.redLine.center.y);
    [UIView animateWithDuration:0.2 animations:^{
        [self.view layoutIfNeeded];
    }];
    self.collectionView.hidden = YES;
    self.tableView.hidden = NO;
    
}


- (void)login:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NYLoginStoryboard" bundle:nil];
    NYLoginViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"NYLoginViewController"];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UICollectionView Delegate


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.topicVM.articlesArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NYRecommendCollectionViewCell *cell = [NYRecommendCollectionViewCell cellWithCollectionView:collectionView indePath:indexPath];
    ArticleListModel *model = [self.topicVM.articlesArray objectAtIndex:indexPath.row];
    cell.model = model;
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"loading"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            if (!CGSizeEqualToSize(model.imageSize, image.size)) {
                model.imageSize = image.size;
                [collectionView reloadItemsAtIndexPaths:@[indexPath]];
            }
        }
    }];
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ArticleListModel *model = [self.topicVM.articlesArray objectAtIndex:indexPath.row];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"RecommendStoryboard" bundle:nil];
    NYArticleDetailViewController *detailVC =[storyboard  instantiateViewControllerWithIdentifier:@"NYArticleDetailViewController"];
    detailVC.articleID = model.articleid;
    [self.navigationController pushViewController:detailVC animated:YES];
}


#pragma mark - CHTCollectionViewDelegateWaterfallLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    ArticleListModel *model = [self.topicVM.articlesArray objectAtIndex:indexPath.row];
    if (!CGSizeEqualToSize(model.imageSize, CGSizeZero)) {
        return model.imageSize;
    }
    return CGSizeMake(150, 150);
}


#pragma mark UITableView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NYTopicListTableViewCell *cell = [NYTopicListTableViewCell cellWithTableView:tableView];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}


//- (BOOL)isLogin{
//    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//    if (delegate.account) {
//        return YES;
//    } else {
//        return NO;
//    }
//}



@end
